const advertDao = require('../../app/facade/advert.facade.js'),
    merchantDao = require('../../app/facade/merchantInfo.facade.js'),
    _ = require('lodash');

module.exports = {
    receiveAdvert: receiveAdvert,
    update: update,
    getBystatus : getBystatus,
    getByNotstatus: getByNotstatus,
    getOnOrdown: getOnOrdown
}

function receiveAdvert(req, res, next){  //cname,reason
    let advert = req.body;
    if (_.isEmpty(advert)) {
        res.json('complaints is empty');
    } else {
        merchantDao.getByIcum(advert.merchant,function(error, data){
            let merchant = data;
            advert.merchant = merchant;
            advertDao.insertOne(advert, (error, result) => {
                if(error) return next(error,req,res);
                res.json(result);
            });
        })
    }
}

function update(req, res, next){
    let advert = req.body;
    if (_.isEmpty(advert)) {
        res.json('advert is empty');
    } else {
        advert.reviewTime = new Date();
        advertDao.updateStatus(advert, (error, result) => {
            if(error) return next(error,req,res);
            res.json(result);
        });
    }
}

function getBystatus(req, res, next){
    let status = req.params.status;
    advertDao.getBystatus(status, (error, result) => {
        if(error) return next(error,req,res);
        res.json(result);
    });
}

function getByNotstatus(req, res, next){
    let status = req.params.status;
    advertDao.getByNotstatus(status, (error, result) => {
        if(error) return next(error,req,res);
        res.json(result);
    });  
}

function getOnOrdown(req, res, next){
    advertDao.getOnOrdown((error, result) => {
        if(error) return next(error,req,res);
        res.json(result);
    });   
}